<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 用户设置
 *
 * @author <judasnow@gmail.com>
 */
class Settings extends CI_Controller{

	private $subject_user_info;

	public function __construct(){
		parent::__construct();
		$this->load->library( 'user_auth' );
		if( $this->user_auth->is_login() ){
			 //已经登录系统
			 //得到用户的信息
			 $user = $this->session->userdata( 'user' );
			 $user_obj = unserialize( $user['user_obj'] );
			 $this->subject_user_info = $user_obj->get();
		 }else{
			 header( 'Location: /' );
			 exit;
		 }
	}

	public function index(){
		 $this->address();
	}

	//用户地址设置
	public function address(){
	/*{{{*/
		//判断用户是否登录 
		//没有登录的用户不能进行购买操作
		if( empty( $this->subject_user_info ) ){
			 //若用户还没有登录系统 
			 header( "Location: /" );
			 exit;		
		}else{
			 $user_id = $this->subject_user_info['user_id'];
			 //获得用户当前存在的地址
			 $this->load->model( 'user_address_model' , '' , TRUE );
			 $user_addresses = $this->user_address_model->find_all( array( 'user_id'=>$user_id ) );
			 //获得当前用户在系统中的信息 ( nick , email )
			 $this->load->model( 'user_model' , '' , TRUE );
                         $local_subject_user_info = $this->user_model->find( array( 'user_id'=>$user_id ) );

                         $respond = $this->session->flashdata( 'respond' );
		}

		$this->load->view( 'settings/address' ,
                        array( 'subject_user_info'=>$this->subject_user_info ,
                                'user_addresses'=>$user_addresses , 
                                'local_subject_user_info'=>$local_subject_user_info ,
                                'respond'=>$respond ) );
		
	}/*}}}*/

	//修改用户的基本信息
	//目前就是指用户的 nick 以及 email
	public function do_modify_user_base_info(){
	/*{{{*/
                if( empty( $this->subject_user_info ) ){
		 
			//若用户还没有登录系统 
		        header( "Location: /" );
			exit;		
		}else{
		        $user_id = $this->subject_user_info['user_id'];
				
			$nick = $this->input->post( 'nick' , TRUE );
			$email = $this->input->post( 'email' , TRUE );

                        $this->load->model( 'user_model' , '' , TRUE );
                        try{
		        	$this->user_model->update( array( 'nick'=>$nick , 'email'=>$email ) , array( 'user_id'=>$user_id ) );
                                //更新成功
                                $this->session->set_flashdata( 'respond' , array( 'res'=>'success' , 'info'=>'设置成功' ) );
                                header( "Location: /settings/address" );
                                exit;	
                        }catch( Exception $e ){
                                $this->session->set_flashdata( 'respond' , array( 'res'=>'error' , 'info'=>'设置失败' ) );
			        header( "Location: /settings/address" );
                                exit;
                        }
		}
	}/*}}}*/

	/**
	 * 添加一条新的地址信息
	 * 如果是用户修改一条已经有的地址信息
	 * 则直接删除原来的 根据地址编号 再写入一条新的
	 * 以简化操作
	 */
	public function do_add_new_address_item(){

		$user_id = $this->subject_user_info['user_id'];

		$this->load->model( 'user_address_model' , '' , TRUE );
		$this->user_address_model->create_and_save_new_address_item( $user_id );

		//添加成功
		header( 'Location: /settings/address' );
		exit();
	}

	//删除一条用户的地址记录
	public function do_delete_address_item(){
	
		$address_no = $this->input->post( 'address_no' , TRUE );
		if( !empty( $address_no ) ){

		 	$this->load->model( 'user_address_model' , '' , TRUE );
			$this->user_address_model->del( array( 'address_no'=>$address_no ) );

			//删除成功
			echo '{"res":"TRUE"}';
			exit;
		}

		echo '{"res":"FALSE"}';
	}

	public function app(){
	/*{{{*/
		//判断用户是否登录 
		if( empty( $this->subject_user_info ) ){		 
			 //若用户还没有登录系统 
			 header( 'Location: /auth/login/' );
			 exit;
		}else{
			 $user_id = $this->subject_user_info['user_id'];
			 //获得用户的app信息
			 $this->load->library( 'Oauth_client' );
			 $server_tokens = $this->oauth_client->list_server_tokens( $user_id );
		}
		
		$this->load->view( 'settings/app' ,
		       	array( 'subject_user_info'=>$this->subject_user_info , 'server_tokens'=>$server_tokens ) );		 
	}/*}}}*/

	public function do_del_app(){
	/*{{{*/

		if( empty( $this->subject_user_info ) ){
		 
			 //若用户还没有登录系统 
			 header( 'Location: /auth/login/' );
			 exit;
		}else{
			 
			 $user_id = $this->subject_user_info['user_id'];
			 
			 $this->load->library( 'Oauth_client' );
			 $servers = $this->oauth_client->list_servers( $user_id );
		}

		$server_uri_host = $this->input->get( 'server_uri_host' );
		$this->oauth_client->do_del_app( $server_uri_host );
	}/*}}}*/
}
